using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using EAT.Common;
using System.Web.Security;
using System.DirectoryServices;
using System.Configuration;

namespace ECTSExtension {

  public class ExtendedUserManager : cUserManager {

      public ExtendedUserManager(string ldapConnectionString)
          : base(ldapConnectionString)
      {
      }

      public ExtendedUserManager()
          : base()
      {
      }

      public void SetProfileInformation(
                        MembershipUser U,
                        string Company,
                        string FirstName,
                        string LastName,
                        string PhoneNumber
                        )
      {

          try
          {
              
              DirectoryEntry de = this.FindUser(U.UserName);

              de.Options.PasswordPort = this.Connection.Port();
              de.Properties["company"].Value = HttpUtility.HtmlEncode(Company);
              de.Properties["givenName"].Value = HttpUtility.HtmlEncode(FirstName);
              de.Properties["sn"].Value = HttpUtility.HtmlEncode(LastName);
              de.Properties["telephoneNumber"].Value = HttpUtility.HtmlEncode(PhoneNumber);

              try
              {
                  string requestSite = ConfigurationManager.AppSettings["RequestSite"].ToString();
                  string adField = ConfigurationManager.AppSettings["RequestSiteAdField"].ToString();
                  bool adFieldMultiValue = bool.Parse(ConfigurationManager.AppSettings["ADFieldMultiValue"].ToString());

                  if (adFieldMultiValue)
                      de.Properties[adField].AddRange(new object[] { requestSite });
                  else
                      de.Properties[adField].Value = requestSite;
              }
              catch { }

              de.CommitChanges();
          }
          catch (Exception e)
          {
              throw e;
          }
      }
  }
}
